//登录
const form = document.querySelector('form')
const nameInp = document.querySelector('.username');
const pwdInp = document.querySelector('.password');
const errBox  = document.querySelector('.error');

form.addEventListener('submit', e =>{
    e = e || window.event;
    try {e.preventDefault()} catch(err){e.returnValue = false};

    const name = nameInp.value;
    const pwd = pwdInp.value;

    if(!name || !pwd) return window.alert('请完整填写表单')

    let data = `username=${name}&password=${pwd}`

    
    $.ajax({
        url:'../php/login.php',
        method:'POST',
        dataType:'json',
        headers:{
          'content-type':'application/x-www-form-urlencoded'
        },
        // token:token,
        data:data,
        success: function(res){
            if(res.code === 0){
                errBox.classList.add('active');
                return;
            }
            setcookie('login',1,3600 )
            window.localStorage.setItem('login',1);
            window.localStorage.setItem('id',res.info["0"].id)
            // window.localStorage.setItem('token',res.token)
        
            const url = window.localStorage.getItem('url');
            window.localStorage.removeItem('url');
            window.location.href = `./${url ? url : 'index'}.html`
        }
    })
   
})





